from flask import Blueprint, jsonify, request, g, session
from models.answer import AnswerModel
from decorators import login_required
from router.forms import AnswerForm
from extension import db

bp = Blueprint('answer', __name__, url_prefix='/answer')

@bp.route('/public', methods=['POST'])
@login_required
def answer():
    form = AnswerForm(request.form)
    if form.validate():
        content = form.content.data
        question_id = form.question_id.data
        ans = AnswerModel(content=content, question_id=question_id, author_id=g.user.id)
        db.session.add(ans)
        db.session.commit()
        return jsonify({'code': 200 })
    else:
        return jsonify({
            'code': 400,
            'message': form.errors
        })